package socksStructure

import (
	"gitee.com/kaylee595/textileGraphGeneration/colorx"
	"github.com/nfnt/resize"
	"image"
	"image/color"
	"image/draw"
)

type SocksBuilder struct {
	// textileGraphImg 纺织图
	textileGraphImg image.Image
}

func NewSocksBuilder(textileGraphImg image.Image) *SocksBuilder {
	return &SocksBuilder{
		textileGraphImg: textileGraphImg,
	}
}

type Option func(leftView, middleView, rightView, backView *image.RGBA)

type SocksBuilderPart struct {
	// BottomImg 底图
	BottomImg image.Image
	// BottomColor 底图颜色修改
	BottomColor color.Color
	// MeshImg 面图
	MeshImg image.Image
	// MeshColor 面图颜色修改
	MeshColor color.Color
	// OverlayImg 需要叠加的图片
	OverlayImgs []image.Image
}

type SocksHeelsBuilderLeft struct {
	SocksBuilderPart
	// 跟角距离
	HITMD int
	// Y 坐标
	Y int
}

type SocksHeelsBuilderRight struct {
	SocksBuilderPart
	// 跟角距离
	HITMD int
	// Y 坐标
	Y int
}

type SocksHeelsBuilderBack struct {
	SocksBuilderPart
	// Y 坐标
	Y int
}

func WithSocksHeelsBuilder(leftOption *SocksHeelsBuilderLeft,
	rightOption *SocksHeelsBuilderRight,
	backOption *SocksHeelsBuilderBack) Option {
	return func(leftView, middleView, rightView, backView *image.RGBA) {
		if leftOption != nil {
			// 先缩放跟的宽度
			//    缩放到宽度为: 跟的宽度/(跟角距/(视图宽度/2))
			width := leftOption.BottomImg.Bounds().Dx()
			width = int(float64(width) / (float64(leftOption.HITMD) / float64(leftView.Bounds().Dx()/2)))
			bottomImg := resize.Resize(uint(width), 0, leftOption.BottomImg, resize.Lanczos3)
			if leftOption.BottomColor != nil {
				bottomImg = colorx.ChangeImgColor(bottomImg, nil, leftOption.BottomColor)
			}
			meshImg := resize.Resize(uint(width), 0, leftOption.MeshImg, resize.Lanczos3)
			if leftOption.MeshColor != nil {
				meshImg = colorx.ChangeImgColor(meshImg, nil, leftOption.MeshColor)
			}
			overlayImgs := make([]image.Image, len(leftOption.OverlayImgs))
			for i := range overlayImgs {
				overlayImgs[i] = resize.Resize(uint(width), 0, leftOption.OverlayImgs[i], resize.Lanczos3)
			}
			// 缩放宽度-视图宽度/2 得出左边需要溢出的空间
			spillageWidth := width - leftView.Bounds().Dx()/2
			// 生成新的画布
			rect := image.Rect(0, 0, leftView.Bounds().Dx()+spillageWidth, leftView.Bounds().Dy())
			newLeftView := image.NewRGBA(rect)
			// 绘制视图
			viewRect := rect
			viewRect.Min.X = spillageWidth
			draw.Draw(newLeftView, viewRect, leftView, image.Point{}, draw.Src)
			// 绘制跟
			viewRect = rect
			viewRect.Min.Y = leftOption.Y
			draw.Draw(newLeftView, viewRect, bottomImg, image.Point{}, draw.Over)
			//draw.Draw(newLeftView, viewRect, meshImg, image.Point{}, draw.Over)
			for _, overlayImg := range overlayImgs {
				draw.Draw(newLeftView, viewRect, overlayImg, image.Point{}, draw.Over)
			}
			draw.Draw(newLeftView, viewRect, meshImg, image.Point{}, draw.Over)
			*leftView = *newLeftView
		}
		if rightOption != nil {
			// 先缩放跟的宽度
			//    缩放到宽度为: 跟的宽度/(跟角距/(视图宽度/2))
			width := rightOption.BottomImg.Bounds().Dx()
			width = int(float64(width) / (float64(rightOption.HITMD) / float64(rightView.Bounds().Dx()/2)))
			bottomImg := resize.Resize(uint(width), 0, rightOption.BottomImg, resize.Lanczos3)
			if rightOption.BottomColor != nil {
				bottomImg = colorx.ChangeImgColor(bottomImg, nil, rightOption.BottomColor)
			}
			meshImg := resize.Resize(uint(width), 0, rightOption.MeshImg, resize.Lanczos3)
			if rightOption.MeshColor != nil {
				meshImg = colorx.ChangeImgColor(meshImg, nil, rightOption.MeshColor)
			}
			overlayImgs := make([]image.Image, len(rightOption.OverlayImgs))
			for i := range overlayImgs {
				overlayImgs[i] = resize.Resize(uint(width), 0, rightOption.OverlayImgs[i], resize.Lanczos3)
			}
			// 缩放宽度-视图宽度/2 得出右边边需要溢出的空间
			spillageWidth := width - rightView.Bounds().Dx()/2
			// 生成新的画布
			rect := image.Rect(0, 0, rightView.Bounds().Dx()+spillageWidth, rightView.Bounds().Dy())
			newRightView := image.NewRGBA(rect)
			// 绘制视图
			draw.Draw(newRightView, rect, rightView, image.Point{}, draw.Src)
			// 绘制跟
			viewRect := rect
			viewRect.Min.X = rightView.Bounds().Dx() / 2
			viewRect.Min.Y = rightOption.Y
			draw.Draw(newRightView, viewRect, bottomImg, image.Point{}, draw.Over)
			//draw.Draw(newRightView, viewRect, meshImg, image.Point{}, draw.Over)
			for _, overlayImg := range overlayImgs {
				draw.Draw(newRightView, viewRect, overlayImg, image.Point{}, draw.Over)
			}
			draw.Draw(newRightView, viewRect, meshImg, image.Point{}, draw.Over)
			*rightView = *newRightView
		}
		if backOption != nil {
			// 先缩放跟的宽度
			//    缩放到宽度为: 视图宽度
			width := backView.Bounds().Dx()
			bottomImg := resize.Resize(uint(width), 0, backOption.BottomImg, resize.Lanczos3)
			if backOption.BottomColor != nil {
				bottomImg = colorx.ChangeImgColor(bottomImg, nil, backOption.BottomColor)
			}
			meshImg := resize.Resize(uint(width), 0, backOption.MeshImg, resize.Lanczos3)
			if backOption.MeshColor != nil {
				meshImg = colorx.ChangeImgColor(meshImg, nil, backOption.MeshColor)
			}
			overlayImgs := make([]image.Image, len(backOption.OverlayImgs))
			for i := range overlayImgs {
				overlayImgs[i] = resize.Resize(uint(width), 0, backOption.OverlayImgs[i], resize.Lanczos3)
			}
			// 绘制跟
			viewRect := image.Rect(0, backOption.Y, width, backView.Bounds().Dy())
			draw.Draw(backView, viewRect, bottomImg, image.Point{}, draw.Over)
			//draw.Draw(backView, viewRect, meshImg, image.Point{}, draw.Over)
			for _, overlayImg := range overlayImgs {
				draw.Draw(backView, viewRect, overlayImg, image.Point{}, draw.Over)
			}
			draw.Draw(backView, viewRect, meshImg, image.Point{}, draw.Over)
		}
	}
}

func WithSocksEndBuilder(leftOption, middleOption, rightOption, backOption *SocksBuilderPart) Option {
	return func(leftView, middleView, rightView, backView *image.RGBA) {
		if leftOption != nil {
			// 先缩放尖的宽度
			width := leftView.Bounds().Dx()
			bottomImg := resize.Resize(uint(width), 0, leftOption.BottomImg, resize.Lanczos3)
			if leftOption.BottomColor != nil {
				bottomImg = colorx.ChangeImgColor(bottomImg, nil, leftOption.BottomColor)
			}
			meshImg := resize.Resize(uint(width), 0, leftOption.MeshImg, resize.Lanczos3)
			if leftOption.MeshColor != nil {
				meshImg = colorx.ChangeImgColor(meshImg, nil, leftOption.MeshColor)
			}
			overlayImgs := make([]image.Image, len(leftOption.OverlayImgs))
			for i := range overlayImgs {
				overlayImgs[i] = resize.Resize(uint(width), 0, leftOption.OverlayImgs[i], resize.Lanczos3)
			}
			// 生成新的画布
			height := leftView.Bounds().Dy()
			rect := image.Rect(0, 0, width, height+bottomImg.Bounds().Dy())
			newLeftView := image.NewRGBA(rect)
			// 绘制视图
			draw.Draw(newLeftView, rect, leftView, image.Point{}, draw.Src)
			// 绘制尖
			viewRect := rect
			viewRect.Min.Y = height
			draw.Draw(newLeftView, viewRect, bottomImg, image.Point{}, draw.Src)
			//draw.Draw(newLeftView, viewRect, meshImg, image.Point{}, draw.Over)
			for _, overlayImg := range overlayImgs {
				draw.Draw(newLeftView, viewRect, overlayImg, image.Point{}, draw.Over)
			}
			draw.Draw(newLeftView, viewRect, meshImg, image.Point{}, draw.Over)
			*leftView = *newLeftView
		}
		if middleOption != nil {
			// 先缩放跟的宽度
			width := middleView.Bounds().Dx()
			bottomImg := resize.Resize(uint(width), 0, middleOption.BottomImg, resize.Lanczos3)
			if middleOption.BottomColor != nil {
				bottomImg = colorx.ChangeImgColor(bottomImg, nil, middleOption.BottomColor)
			}
			meshImg := resize.Resize(uint(width), 0, middleOption.MeshImg, resize.Lanczos3)
			if middleOption.MeshColor != nil {
				meshImg = colorx.ChangeImgColor(meshImg, nil, middleOption.MeshColor)
			}
			overlayImgs := make([]image.Image, len(middleOption.OverlayImgs))
			for i := range overlayImgs {
				overlayImgs[i] = resize.Resize(uint(width), 0, middleOption.OverlayImgs[i], resize.Lanczos3)
			}
			// 生成新的画布
			height := middleView.Bounds().Dy()
			rect := image.Rect(0, 0, width, height+bottomImg.Bounds().Dy())
			newMiddleView := image.NewRGBA(rect)
			// 绘制视图
			draw.Draw(newMiddleView, rect, middleView, image.Point{}, draw.Src)
			// 绘制尖
			viewRect := rect
			viewRect.Min.Y = height
			draw.Draw(newMiddleView, viewRect, bottomImg, image.Point{}, draw.Over)
			//draw.Draw(newMiddleView, viewRect, meshImg, image.Point{}, draw.Over)
			for _, overlayImg := range overlayImgs {
				draw.Draw(newMiddleView, viewRect, overlayImg, image.Point{}, draw.Over)
			}
			draw.Draw(newMiddleView, viewRect, meshImg, image.Point{}, draw.Over)
			*middleView = *newMiddleView
		}
		if rightOption != nil {
			// 先缩放跟的宽度
			width := rightView.Bounds().Dx()
			bottomImg := resize.Resize(uint(width), 0, rightOption.BottomImg, resize.Lanczos3)
			if rightOption.BottomColor != nil {
				bottomImg = colorx.ChangeImgColor(bottomImg, nil, rightOption.BottomColor)
			}
			meshImg := resize.Resize(uint(width), 0, rightOption.MeshImg, resize.Lanczos3)
			if rightOption.MeshColor != nil {
				meshImg = colorx.ChangeImgColor(meshImg, nil, rightOption.MeshColor)
			}
			overlayImgs := make([]image.Image, len(rightOption.OverlayImgs))
			for i := range overlayImgs {
				overlayImgs[i] = resize.Resize(uint(width), 0, rightOption.OverlayImgs[i], resize.Lanczos3)
			}
			// 生成新的画布
			height := rightView.Bounds().Dy()
			rect := image.Rect(0, 0, width, height+bottomImg.Bounds().Dy())
			newRightView := image.NewRGBA(rect)
			// 绘制视图
			draw.Draw(newRightView, rect, rightView, image.Point{}, draw.Src)
			// 绘制尖
			viewRect := rect
			viewRect.Min.Y = height
			draw.Draw(newRightView, viewRect, bottomImg, image.Point{}, draw.Over)
			//draw.Draw(newRightView, viewRect, meshImg, image.Point{}, draw.Over)
			for _, overlayImg := range overlayImgs {
				draw.Draw(newRightView, viewRect, overlayImg, image.Point{}, draw.Over)
			}
			draw.Draw(newRightView, viewRect, meshImg, image.Point{}, draw.Over)
			*rightView = *newRightView
		}
		if backOption != nil {
			// 先缩放跟的宽度
			width := backView.Bounds().Dx()
			bottomImg := resize.Resize(uint(width), 0, backOption.BottomImg, resize.Lanczos3)
			if backOption.BottomColor != nil {
				bottomImg = colorx.ChangeImgColor(bottomImg, nil, backOption.BottomColor)
			}
			meshImg := resize.Resize(uint(width), 0, backOption.MeshImg, resize.Lanczos3)
			if backOption.MeshColor != nil {
				meshImg = colorx.ChangeImgColor(meshImg, nil, backOption.MeshColor)
			}
			overlayImgs := make([]image.Image, len(backOption.OverlayImgs))
			for i := range overlayImgs {
				overlayImgs[i] = resize.Resize(uint(width), 0, backOption.OverlayImgs[i], resize.Lanczos3)
			}
			// 生成新的画布
			height := backView.Bounds().Dy()
			rect := image.Rect(0, 0, width, height+bottomImg.Bounds().Dy())
			newBackView := image.NewRGBA(rect)
			// 绘制视图
			draw.Draw(newBackView, rect, backView, image.Point{}, draw.Src)
			// 绘制尖
			viewRect := rect
			viewRect.Min.Y = height
			draw.Draw(newBackView, viewRect, bottomImg, image.Point{}, draw.Over)
			//draw.Draw(newBackView, viewRect, meshImg, image.Point{}, draw.Over)
			for _, overlayImg := range overlayImgs {
				draw.Draw(newBackView, viewRect, overlayImg, image.Point{}, draw.Over)
			}
			draw.Draw(newBackView, viewRect, meshImg, image.Point{}, draw.Over)
			*backView = *newBackView
		}
	}
}

func (s *SocksBuilder) Build(options ...Option) (leftView, middleView, rightView, backView *image.RGBA) {
	// 生成袜子的四个结构: 左图, 前图, 右图, 背图
	// 将图片分为4个部分:
	// 1+2的部分是左侧图
	// 2+3的部分为中图
	// 3+4的部分是右侧图
	// 4+1的部分为背图
	bounds := s.textileGraphImg.Bounds()
	subWidth := bounds.Max.X / 4
	subRect := image.Rect(0, 0, subWidth*2, bounds.Max.Y)
	leftView = image.NewRGBA(subRect)
	middleView = image.NewRGBA(subRect)
	rightView = image.NewRGBA(subRect)
	backView = image.NewRGBA(subRect)
	draw.Draw(leftView, subRect, s.textileGraphImg, image.Point{}, draw.Src)
	draw.Draw(middleView, subRect, s.textileGraphImg, image.Point{X: subWidth}, draw.Src)
	draw.Draw(rightView, subRect, s.textileGraphImg, image.Point{X: subWidth * 2}, draw.Src)
	draw.Draw(backView, image.Rect(0, 0, subWidth, bounds.Max.Y), s.textileGraphImg, image.Point{X: subWidth * 3}, draw.Src)
	draw.Draw(backView, image.Rect(subWidth, 0, subWidth*2, bounds.Max.Y), s.textileGraphImg, image.Point{}, draw.Src)
	for _, option := range options {
		option(leftView, middleView, rightView, backView)
	}
	return
}
